package com.wu.controller;

import com.wu.response.CommonReturnType;
import com.wu.service.ArticleDraftService;
import com.wu.service.ArticlesService;
import com.wu.service.LikeArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import static com.wu.constant.Constant.*;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-23 16:36
 */
@RestController
@RequestMapping("/article")
@Api(tags = "主要文章接口")
public class ArticlesController {
    @Autowired
    private ArticlesService articlesService;

    @Autowired
    private LikeArticleService likeArticleService;

    @Autowired
    private ArticleDraftService articleDraftService;


    /**
     * 通过文章Id返回文章信息
     *
     * @param articleId
     * @return
     */
    @ApiOperation(value = "根据文章Id获取文章")
    @ApiImplicitParams({@ApiImplicitParam(name = "articleId", value = "文章ID", required = true, dataTypeClass = String.class)})
    @RequestMapping(value = "/get/{articleId}", method = RequestMethod.GET)
    public CommonReturnType getArticleById(@PathVariable("articleId") String articleId) {
        Map<Object, Object> articleById = articlesService.getArticleById(articleId);
        return CommonReturnType.create(articleById);
    }


    /**
     * 根据一些查询条件返回对应的文章
     *
     * @param classify
     * @param p
     * @return
     */
    @ApiOperation(value = "根据一些查询条件返回对应的文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classify", value = "文章专栏默认为default", defaultValue = "default", dataTypeClass = String.class),
            @ApiImplicitParam(name = "p", value = "当前页数，默认为1", dataTypeClass = String.class),
            @ApiImplicitParam(name = "userId", value = "用户ID，如果传了就查看某个用户的，不传就查看全部用户", dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleWay", value = "0 原创   1  转载  2 翻译", dataTypeClass = String.class)})
    @RequestMapping(value = "/getArticleBySomething", method = RequestMethod.POST)
    public CommonReturnType getArticleBySomeThing(@RequestParam(name = "classify", required = false, defaultValue = DEFAULT_) String classify,
                                                  @RequestParam(name = "p", required = false, defaultValue = ONE) String p,
                                                  @RequestParam(name = "userId", required = false, defaultValue = DEFAULT_NULL_VALUE) String userId,
                                                  @RequestParam(name = "articleWay", required = false, defaultValue = DEFAULT_NULL_VALUE) String articleWay) {
        if (StringUtils.isEmpty(classify)) {
            classify = DEFAULT_;
        }
        if (StringUtils.isEmpty(p)) {
            //m默认为第一页
            p = ONE;
        }
        HashMap<String, Object> article = articlesService.getArticleBySomeThing(classify, p, userId, articleWay);
        return CommonReturnType.create(article);
    }


    /**
     * 文章操作
     *
     * @param classifyId     分类Id
     * @param labelNames     标签拼接
     * @param articleContent 文章内容
     * @param articleTitle   文章标题
     * @param articleWay     0 原创   1  转载  2 翻译
     * @param diyClassify    自定义分类  （如果它不为空，classIfyId必为空）
     * @param articleStatus  文章状态  0 ： 不为草稿  1： 草稿
     * @param articleId      文章Id  ： 1：如果为空就说明是直接发布 ，  2：如果不为空  ：状体0：文章更新 1；发布草稿并删除草稿
     * @return
     */
    @ApiOperation(value = "文章的操作，保存、发布、草稿。。。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classifyId", value = "分类Id", dataTypeClass = String.class),
            @ApiImplicitParam(name = "labelNames", value = "标签拼接，用 = 拼接", dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleContent", value = "文章内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleTitle", value = "文章标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleWay", value = "0 原创   1  转载  2 翻译", dataTypeClass = String.class),
            @ApiImplicitParam(name = "diyClassify", value = "自定义分类（如果它不为空，classIfyId必为空）", dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleStatus", value = "文章状态  0 ： 不为草稿  1： 草稿", dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleId", value = "文章Id : 1 > 如果为空就说明是直接发布 ，  2 > 如果不为空  ：状态 0：文章更新 1；发布草稿并删除草稿", dataTypeClass = String.class)})
    @Transactional(rollbackFor = RuntimeException.class)
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    public CommonReturnType tokenAddArticle(
            @RequestParam(name = "classifyId", required = false, defaultValue = DEFAULT_NULL_VALUE) String classifyId,
            @RequestParam(name = "labelNames", required = false, defaultValue = DEFAULT_NULL_VALUE) String labelNames,
            @RequestParam(name = "articleContent") String articleContent,
            @RequestParam(name = "articleTitle") String articleTitle,
            @RequestParam(name = "articleWay", required = false, defaultValue = ZERO) String articleWay,
            @RequestParam(name = "diyClassify", required = false, defaultValue = DEFAULT_NULL_VALUE) String diyClassify,
            @RequestParam(name = "articleStatus", required = false, defaultValue = ZERO) String articleStatus,
            @RequestParam(name = "articleId", required = false, defaultValue = DEFAULT_NULL_VALUE) String articleId
    ) {
        String id;
        if (StringUtils.isEmpty(articleId)) {
            //说明是直接发布文章
            id = articlesService.insertArticle(articleTitle, articleContent, classifyId, labelNames, articleWay, diyClassify);
        } else {
            //这个时候就要判断状态
            if (ONE.equals(articleStatus)) {
                //1 ：发布草稿，并要删除旧草稿
                id = articlesService.insertArticle(articleTitle, articleContent, classifyId, labelNames, articleWay, diyClassify);
                articleDraftService.deleteArticleDraftByArticleId(articleId);
            } else {
                //0： 不是草稿，已发布的文章更新
                id = articlesService.updateArticle(articleId, articleTitle, articleContent, classifyId, labelNames, articleWay, diyClassify);
            }
        }
        return CommonReturnType.create(id);
    }


    /**
     * 编辑文章
     *
     * @param articleId
     * @param classifyId
     * @param labelNames
     * @param articleContent
     * @param articleTitle
     * @return
     */
    @ApiOperation(value = "更新文章")
    @RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
    @Deprecated
    public CommonReturnType updateArticle(@RequestParam("articleId") String articleId,
                                          @RequestParam("classifyId") String classifyId,
                                          @RequestParam("labelNames") String labelNames,
                                          @RequestParam("articleContent") String articleContent,
                                          @RequestParam("articleTitle") String articleTitle,
                                          @RequestParam(name = "articleWay", required = false) String articleWay,
                                          @RequestParam(name = "diyClassify", required = false) String diyClassify) {
        String articleID = articlesService.updateArticle(articleId, articleTitle, articleContent, classifyId, labelNames, articleWay, diyClassify);
        return CommonReturnType.create(articleID);
    }

    /**
     * 给文章点赞或取消点赞
     *
     * @param articleId
     * @return
     */
    @ApiOperation(value = "给文章点赞或取消点赞")
    @ApiImplicitParams({@ApiImplicitParam(name = "articleId", value = "文章ID", required = true, dataTypeClass = String.class)})
    @RequestMapping(value = "/likeArticle", method = RequestMethod.POST)
    public CommonReturnType tokenLikeArticle(@RequestParam("articleId") String articleId) {
        Map<Object, Object> map = likeArticleService.addOrDeleteLike(articleId);
        return CommonReturnType.create(map);
    }

    /**
     * 通过文章Id删除与文章相关的一切信息
     *
     * @param articleId
     * @return
     */
    @ApiOperation(value = "通过文章Id删除文章")
    @ApiImplicitParams({@ApiImplicitParam(name = "articleId", value = "文章ID", required = true, dataTypeClass = String.class)})
    @RequestMapping(value = "deleteArticleByArticleId", method = RequestMethod.POST)
    public CommonReturnType tokenDeleteArticle(@RequestParam("articleId") String articleId) {
        articlesService.deleteArticleByArticleId(articleId);
        return CommonReturnType.create(null);
    }


}
